<template>
	<view v-if="isHide">
		<view v-if="canIUse">
			<view class='header'>
				<image src='/static/icon/wx_login.png'></image>
			</view>

			<view class='content'>
				<view>申请获取以下权限</view>
				<text>获得你的公开信息(昵称，头像等)</text>
			</view>

			<button class='bottom' type='primary' open-type="getUserInfo" @getuserinfo="getUserInfo">
				授权登录
			</button>
		</view>
	</view>

</template>

<script>
	import {
		getSetting
	} from '@/utils/login.js'
	export default {
		data() {
			return {
				canIUse: uni.canIUse('button.open-type.getUserInfo'),
				isHide: true,
				//请求参数
				userInfo: {}
			}
		},
		mounted() {},
		methods: {
			resh(e) {
				this.$emit("send", e)
			},
			saveUserInfo() {
				this.request({
					url: this.httpServer + '/v1/app/userInfo/',
					data: this.userInfo,
					method: "post"
				}).then(res => {
					if (res.code === 200) {
						uni.setStorage({
							key: "deviceId",
							data: res.msg
						})
						this.resh(false)
					} else {
						uni.showToast({
							title: '授权登录失败！',
							mask: true,
							icon: 'none'
						})
					}
				})
			},
			getUserInfo(e) {
				getSetting().then(isAuth => {
					uni.login({
						success: res => {
							this.userInfo.code = res.code;
							if (isAuth) {
								this.userInfo.userInfo = e.detail.userInfo
								this.userInfo.iv = e.detail.iv
								this.userInfo.encryptedData = e.detail.encryptedData
								this.userInfo.signature = e.detail.signature
								this.saveUserInfo();
							} else {
								uni.showToast({
									title: '授权失败，请确认授权已开启',
									mask: true,
									icon: 'none'
								})
							}
						}
					})
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.header {
		margin: 90rpx 0 90rpx 50rpx;
		border-bottom: 1px solid #ccc;
		text-align: center;
		width: 650rpx;
		height: 300rpx;
		line-height: 450rpx;
		display: flex;
		justify-content: center;
	}

	.header image {
		width: 200rpx;
		height: 200rpx;
	}

	.content {
		margin-left: 50rpx;
		margin-bottom: 90rpx;
	}

	.content text {
		display: block;
		color: #9d9d9d;
		margin-top: 40rpx;
	}

	.bottom {
		border-radius: 80rpx;
		margin: 70rpx 50rpx;
		font-size: 35rpx;
	}
</style>
